package controller.invoice;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import model.Invoice;
import model.InvoiceDetail;
import util.Exporter;
import view.invoice.InvoiceViewPanel;
import dao.InvoiceDAO;

public class InvoiceViewController implements ActionListener, Observer,
		MouseListener {

	@Override
	public void update(Observable o, Object arg) {
		loadInvoices();
	}

	private InvoiceViewPanel gui;
	private InvoiceDAO invoiceDAO;
	private DefaultTableModel dtm;
	private DefaultTableModel idDtm;
	private List<Invoice> list;

	private JFileChooser chooser;
	private Invoice invoice;

	private int position;

	public InvoiceViewController(InvoiceViewPanel gui) {

		chooser = new JFileChooser();

		chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

		this.gui = gui;
		this.invoiceDAO = new InvoiceDAO();
		loadInvoices();
	}

	private void setNewInvoiceDetail() {
		resetDetailTableModel();
		position = gui.getTblInnviewListinvoice().getSelectedRow();
		invoice = list.get(position);
		Iterator<InvoiceDetail> ivds = invoice.getInvoiceDetail().iterator();

		while (ivds.hasNext()) {
			InvoiceDetail invoiceDetail = ivds.next();
			this.idDtm.addRow(new Object[] { invoiceDetail.getProductID(),
					invoiceDetail.getProductName(),
					invoiceDetail.getQuantity(), invoiceDetail.getPrice() });
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// setNewInvoiceDetail();
	}

	@Override
	public void mouseEntered(MouseEvent e) {

	}

	@Override
	public void mouseExited(MouseEvent e) {

	}

	@Override
	public void mousePressed(MouseEvent e) {

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		setNewInvoiceDetail();
	}

	private void resetDefaultTableModel() {
		this.dtm = new DefaultTableModel();
		dtm.addColumn("Invoice Id");
		dtm.addColumn("Date");
		dtm.addColumn("Customer Id");
		dtm.addColumn("Customer Name");
		dtm.addColumn("Total");

		resetDetailTableModel();
	}

	private void resetDetailTableModel() {
		this.idDtm = new DefaultTableModel();
		idDtm.addColumn("Product Id");
		idDtm.addColumn("Product Name");
		idDtm.addColumn("Quantity");
		idDtm.addColumn("Price");
		gui.getTblInvviewInvoicedetail().setModel(this.idDtm);
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		if (event.getSource() == gui.getBtnInvviewDelete()) {
			deleteInvoice(event);
		} else if (event.getSource() == gui.getBtnInvviewSearch()) {
			searchInvoice(event);
		} else if (event.getSource() == gui.getBtnInvviewPrint()) {
			print();
		}

	}

	private void print() {
		if (invoice != null) {
			int choice = chooser.showSaveDialog(gui);
			if (choice == JFileChooser.APPROVE_OPTION) {
				File folderOut = chooser.getSelectedFile();
				if(Exporter.exportFile(invoice, folderOut.getAbsolutePath()))
					JOptionPane.showMessageDialog(gui,
							"Your file is exported", "Message",
							JOptionPane.INFORMATION_MESSAGE);
			}
		} else {
			JOptionPane.showMessageDialog(gui,
					"Please choose an invoice to print", "Message",
					JOptionPane.INFORMATION_MESSAGE);
		}
	}

	private void searchInvoice(ActionEvent event) {
		String key = gui.getTxtInvviewSearch().getText();
		String select = gui.getCbbInvviewSearchby().getSelectedItem()
				.toString();
		this.dtm = new DefaultTableModel();
		dtm.addColumn("Invoice Id");
		dtm.addColumn("Date");
		dtm.addColumn("Customer Id");
		dtm.addColumn("Customer Name");
		dtm.addColumn("Total");
		switch (select) {
		case "InvoiceID":
			for (Invoice invoice : invoiceDAO.showAllInvoiceById(key)) {
				dtm.addRow(new Object[] { invoice.getInvoiceID(),
						invoice.getDate(), invoice.getCustomerID(),
						invoice.getCustomerName(), invoice.getTotal() });
			}
			gui.getTblInnviewListinvoice().setModel(dtm);
			break;

		case "Orther":
			for (Invoice invoice : invoiceDAO.showAllInvoiceByAll(key)) {
				dtm.addRow(new Object[] { invoice.getInvoiceID(),
						invoice.getDate(), invoice.getCustomerID(),
						invoice.getCustomerName(), invoice.getTotal() });
			}
			gui.getTblInnviewListinvoice().setModel(dtm);
			break;
		}
	}

	private void loadInvoices() {
		resetDefaultTableModel();
		list = invoiceDAO.showAllInvoice();
		for (Invoice invoice : list) {
			dtm.addRow(new Object[] { invoice.getInvoiceID(),
					invoice.getDate(), invoice.getCustomerID(),
					invoice.getCustomerName(), invoice.getTotal() });
		}
		gui.getTblInnviewListinvoice().setModel(dtm);
	}

	private void deleteInvoice(ActionEvent event) {
		invoiceDAO.deleteInvoice(list.get(position));
		JOptionPane.showMessageDialog(null, "delete Invoice successfully");
		loadInvoices();
	}
}
